package com.dmap.admin.controller.backgroundv2;

import com.dmap.admin.controller.backgroundv2.param.UserToPermissionParam;
import com.dmap.admin.service.backgroundv2.UserToPermissionManageService;
import com.dmap.admin.service.backgroundv2.UserToPermissionService;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 04/05/18
 * \* Description:
 * \
 */
@Api(value = "V2用户权限管理", description = "background/V2用户权限管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v2/userToPermission/")
public class UserToPermissionController {

    @Resource
    private UserToPermissionService userToPermissionService;
    @Resource
    private UserToPermissionManageService userToPermissionManageService;

    @ResponseBody
    @ApiOperation(value = "新增or修改 用户权限",
            notes = "1. 必填： userId, 权限编号permissionNumber;")
    @RequestMapping(value = "insert/insertOrUpdate", method = RequestMethod.POST)
    public BaseVo insertOrUpdate(@RequestBody List<UserToPermissionParam> paramList) {
        BaseVo baseVo = new BaseVo();
        if (CollectionUtils.isEmpty(paramList) || StringUtils.isBlank(paramList.get(0).getUserId())
                || !StringUtils.isNumeric(paramList.get(0).getUserId()) || Long.valueOf(paramList.get(0).getUserId()).equals(0L)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        userToPermissionService.deleteByUserId(Long.valueOf(paramList.get(0).getUserId()));
        for (UserToPermissionParam param : paramList) {
            userToPermissionService.insert(param);
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据userId查询用户权限列表", notes = "1. 必填： userId;")
    @RequestMapping(value = "select/selectByUserId", method = RequestMethod.GET)
    public BaseVo selectByUserId(@RequestParam String userId) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(userId) || !StringUtils.isNumeric(userId) || Long.valueOf(userId).equals(0L)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        baseVo.setData(userToPermissionManageService.getUserToPermissionTreeListByUserId(Long.valueOf(userId)));
        return baseVo;
    }

}